package com.leesche.zcsp.node.web.controller.web.v2.place;

import com.leesche.zcsp.node.utils.web.messge.WebResult;
import com.leesche.zcsp.node.web.controller.web.v2.admin.WebAdminController;
import com.leesche.zcsp.node.web.model.PubPlace;
import com.leesche.zcsp.node.web.service.PubDeviceService;
import com.leesche.zcsp.node.web.service.PubPlaceService;
import com.leesche.zcsp.node.web.service.impl.data.read.InfoDeviceDto;
import com.leesche.zcsp.node.web.service.impl.data.read.PubPlaceDto;
import com.leesche.zcsp.node.web.service.impl.data.read.QueryPubDeviceCondition;
import com.leesche.zcsp.node.web.service.impl.data.read.QueryPubPlaceCondition;
import com.leesche.zcsp.node.web.vo.v2.PlaceDeviceListVo;
import com.leesche.zcsp.node.web.vo.v2.enums.DeviceEnum;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * Created by kaider on 2016/12/2.
 */
@Controller
@RequestMapping("/v2/place")
public class WebPlaceController extends WebAdminController {

    private final static Logger LOGGER = LoggerFactory.getLogger(WebPlaceController.class);

    private static final String SHOW_VIEW = "web/v2/place/showView";

    @Autowired
    private PubPlaceService placeService;
    @Autowired
    private PubDeviceService deviceService;


    @RequestMapping(value = "/showView", method = RequestMethod.GET)
    public String showView(
        QueryPubPlaceCondition placeCondition,
        Model model
    ) {
        goPlaceView(placeCondition,model);
        return SHOW_VIEW;
    }

    /**
     * 某个场地设备列表
     * @param placeId
     * @param model
     * @return
     */
    @RequestMapping(value = "/{placeId}/placeView", method = RequestMethod.GET)
    public String showPlaceView(
        @PathVariable("placeId") Long placeId,
        QueryPubPlaceCondition placeCondition,
        QueryPubDeviceCondition deviceCondition,
        Model model
    ){
        Long adminId = getAdminId();
        placeCondition.setAdminId(adminId);
        deviceCondition.setAdminId(adminId);
        List<PubPlaceDto> placeList = placeService.findPubPlaceList(placeCondition);
        PubPlaceDto dto = placeService.findByCondition(placeCondition);
        List<InfoDeviceDto> deviceList = deviceService.findDeviceList(deviceCondition);
        PlaceDeviceListVo vo = getPlaceDeviceListVo(deviceList);
        model.addAttribute("placeList",placeList);
        model.addAttribute("placeDto",dto);
        model.addAttribute("placeId",placeId);
        model.addAttribute("giftList",vo.getGiftList());
        model.addAttribute("currencyList",vo.getCurrencyList());
        model.addAttribute("dollList",vo.getDollList());
        model.addAttribute("giftExchangeList",vo.getGiftExchangeList());
        model.addAttribute("view",1);
        return SHOW_VIEW;
    }

    /**
     * 更改场地名称
     * @param placeId
     * @param placeName
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/{placeId}/editPlaceName", method = RequestMethod.POST)
    public Map<String,Object> editPlaceName(
        @PathVariable("placeId") Long placeId,
        String placeName
    ){
        try {
            PubPlace oldPlace = placeService.selectByPrimaryKey(placeId);
            oldPlace.setAddress(placeName);
            placeService.updateByPrimaryKeySelective(oldPlace);
            return WebResult.success().toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

    /**
     * 查询页面
     * @param model
     * @return
     */
    @RequestMapping(value = "/searchView", method = RequestMethod.GET)
    public String searchView(
        QueryPubPlaceCondition placeCondition,
        Model model
    ){
        goPlaceView(placeCondition,model);
        model.addAttribute("view",2);
        return SHOW_VIEW;
    }

    /**
     * 场地查询设备
     *
     * @param deviceCondition
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/searchPlaceDevice", method = RequestMethod.POST)
    public Map<String, Object> searchPlaceDevice(
        QueryPubDeviceCondition deviceCondition
    ) {
        try {
            Long adminId = getAdminId();
            deviceCondition.setAdminId(adminId);
            List<InfoDeviceDto> deviceList = deviceService.findDeviceList(deviceCondition);
            return WebResult.success().put("data", deviceList).toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }

    /**
     * 条件查询设备
     * @param placeCondition
     * @param deviceCondition
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/searchDevice", method = RequestMethod.POST)
    public Map<String,Object> searchDevice(
            QueryPubPlaceCondition placeCondition,
            QueryPubDeviceCondition deviceCondition
    ){
        Long adminId = getAdminId();
        deviceCondition.setAdminId(adminId);
        placeCondition.setAdminId(adminId);
        List<PlaceDeviceListVo> placeDevicesList = new ArrayList<PlaceDeviceListVo>();
        try {
            if(StringUtils.isNotBlank(deviceCondition.getImme()) ||
                StringUtils.isNotBlank(deviceCondition.getDeviceshortcode()) ||
                deviceCondition.getDeviceId()!=0){

                List<InfoDeviceDto> deviceTempList = deviceService.findDeviceList(deviceCondition);
                List<Long> placeIdList = new ArrayList<Long>();
                for(InfoDeviceDto dto:deviceTempList){
                    placeCondition.setPlaceId(dto.getPlaceId());
                    PubPlaceDto placeDto = placeService.findByCondition(placeCondition);
                    placeIdList.add(placeDto.getPlaceid());
                }
                //场地id去重
                placeIdList = new ArrayList(new HashSet(placeIdList));
                //设置场地
                for(int i=0;i<placeIdList.size();i++){
                    Long placeId = placeIdList.get(i);
                    placeCondition.setPlaceId(placeId);
                    PubPlaceDto placeDto = placeService.findByCondition(placeCondition);
                    List<InfoDeviceDto> deviceList = new ArrayList<InfoDeviceDto>();
                    for(InfoDeviceDto dto:deviceTempList){
                        if(dto.getPlaceId().equals(placeId)){
                            deviceList.add(dto);
                        }
                    }
                    PlaceDeviceListVo vo = getPlaceDeviceListVo(deviceList);
                    vo.setPlaceDto(placeDto);
                    placeDevicesList.add(vo);
                }
            }else{
                if(deviceCondition.getPlaceId()==0 && deviceCondition.getDeviceId()==0){
                    List<PubPlaceDto> placeList = placeService.findPubPlaceList(placeCondition);
                    for(PubPlaceDto dto : placeList){
                        deviceCondition.setPlaceId(dto.getPlaceid());
                        List<InfoDeviceDto> deviceList = deviceService.findDeviceList(deviceCondition);
                        PlaceDeviceListVo vo = getPlaceDeviceListVo(deviceList);
                        vo.setPlaceDto(dto);
                        placeDevicesList.add(vo);
                    }
                }else{
                    PubPlaceDto dto = placeService.findByCondition(placeCondition);
                    deviceCondition.setPlaceId(dto.getPlaceid());
                    List<InfoDeviceDto> deviceList = deviceService.findDeviceList(deviceCondition);
                    PlaceDeviceListVo vo = getPlaceDeviceListVo(deviceList);
                    vo.setPlaceDto(dto);
                    placeDevicesList.add(vo);
                }
            }
            return WebResult.success().put("data",placeDevicesList).toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }



    /**
     * 添加场地
     * @param placeName
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addPlace", method = RequestMethod.POST)
    public Map<String, Object> addPlace(
        String placeName
    ){
        try {
            Long adminId = getAdminId();
            PubPlace place = new PubPlace();
            place.setAdminid(adminId);
            place.setAddress(placeName);
            place.setState(1);
            placeService.insert(place);
            return WebResult.success().toMap();
        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            return WebResult.failure(e).toMap();
        }
    }


    /**
     * 跳转
     * @param placeCondition
     * @param model
     * @return
     */
    private Model goPlaceView(
        QueryPubPlaceCondition placeCondition,
        Model model
    ){
        Long adminId = getAdminId();
        placeCondition.setAdminId(adminId);
        List<PubPlaceDto> placeList = placeService.findPubPlaceList(placeCondition);
        model.addAttribute("placeList",placeList);
        return model;
    }

    /**
     * 设备分类
     * @param deviceList
     * @return
     */
    private PlaceDeviceListVo getPlaceDeviceListVo(
        List<InfoDeviceDto> deviceList
    ){
        PlaceDeviceListVo vo = new PlaceDeviceListVo();
        //礼品机
        List<InfoDeviceDto> giftList = new ArrayList<InfoDeviceDto>();
        //兑币机
        List<InfoDeviceDto> currencyList = new ArrayList<InfoDeviceDto>();
        //娃娃机
        List<InfoDeviceDto> dollList = new ArrayList<InfoDeviceDto>();
        //礼品兑换机
        List<InfoDeviceDto> giftExchangeList = new ArrayList<InfoDeviceDto>();

        for(InfoDeviceDto deviceDto : deviceList){
            String stateName = DeviceEnum.parse(deviceDto.getState()).getMessage();
            deviceDto.setStateName(stateName);
            switch (deviceDto.getDeviceTypeId().intValue()){
                //礼品
                case 1:
                    giftList.add(deviceDto);
                    break;
                //兑币
                case 2:
                    currencyList.add(deviceDto);
                    break;
                //娃娃
                case 3:
                    dollList.add(deviceDto);
                    break;
                //礼品兑换
                case 4:
                    giftExchangeList.add(deviceDto);
                    break;
                default:
                    break;
            }
        }
        vo.setCurrencyList(currencyList);
        vo.setDollList(dollList);
        vo.setGiftList(giftList);
        vo.setGiftExchangeList(giftExchangeList);

        return vo;
    }

}
